/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Copyright (c): Johannes Kuhaupt
 * Licence: MIT
 */
package control.boo;

import control.GuiManager;
import control.SettingsManager;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Johannes Kuhaupt
 */
public class BooInterpreter extends AbstractBooTask {
    
    

    /**
     *
     * @param pFile
     */
    public BooInterpreter(String pFile) {
        this.taskFile = new File(pFile);
    }

    /**
     *
     */
    @Override
    public void run() {
        this.guiMan.setBooReady(false);
        this.startInterpreting();
    }

    private void startInterpreting() {
        compilerResult = "";
        currentProcess = null;
        processStarted = false;

        if (SettingsManager.getBooleanSettingValue("MONO")) {
            this.startBooTaskWithMono();
        } else {
            this.startBooTaskWithNet();
        }

        if (processStarted) {
            this.readOutputStream();
            this.guiMan.setBooReady(true);

            //if (!this.compilerResult.contains("Ende der internen Ausnahmestapel")) {
                //this.guiMan.setOutputInFrame(GuiManager.getLangBundle().getString("MsgSuccessInterpreting"));
               // this.guiMan.setStatusInfo(GuiManager.getLangBundle().getString("StatusMsgInterpretingSuccess"));
            //} else {
                this.guiMan.setOutputInFrame(this.compilerResult);
                //this.guiMan.setStatusInfo(GuiManager.getLangBundle().getString("StatusMsgInterpretingError"));
                this.guiMan.setStatusInfo("Interpretiere Skript");
                //this.guiMan.showOutputTab();
           // }

        }

    }
    
    private void startBooTaskWithMonoAndLinuxShellScript(File booExe, File monoExe){
        if (booExe.exists() && booExe.canExecute() && booExe.getName().equals("booi") && monoExe.exists() && monoExe.canExecute() && (monoExe.getName().equals("mono.exe") || monoExe.getName().equals("mono"))) {
            pb = new ProcessBuilder(pathToBoo, taskFile.getName());
            pb.redirectErrorStream(true);
            pb.directory(path);

            try {
                currentProcess = pb.start();
                processStarted = true;
            } catch (IOException ex) {
                this.guiMan.setOutputInFrame(this.compilerResult);
                this.guiMan.setBooReady(true);
                this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorInterpreting"));
                Logger.getLogger(BooInterpreter.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Error, startBooTask failed (Interpreter): " + ex.toString());
            }

        } else {
            this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorBooi"));
            this.guiMan.setBooReady(true);
        }
    }
    
    private void startBooTaskWithLinxAndMonoAndBooExe(File booExe, File monoExe){
        if (booExe.exists() && booExe.canExecute() && booExe.getName().equals("booi.exe") && monoExe.exists() && monoExe.canExecute() && (monoExe.getName().equals("mono.exe") || monoExe.getName().equals("mono"))) {
            pb = new ProcessBuilder(pathToMono, pathToBoo, taskFile.getName());
            pb.redirectErrorStream(true);
            pb.directory(path);

            try {
                currentProcess = pb.start();
                processStarted = true;
            } catch (IOException ex) {
                this.guiMan.setOutputInFrame(this.compilerResult);
                this.guiMan.setBooReady(true);
                this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorInterpreting"));
                Logger.getLogger(BooInterpreter.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Error, startBooTask failed (Interpreter): " + ex.toString());
            }

        } else {
            this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorBooi"));
            this.guiMan.setBooReady(true);
        }
    }

    private void startBooTaskWithMono() {

        path = new File(taskFile.getParent());

        

        File booExe = new File(pathToBoo);
        File monoExe = new File(pathToMono);
        
        if(booExe.getName().equals("booi.exe")){
            this.startBooTaskWithLinxAndMonoAndBooExe(booExe,monoExe );
        }
        else if(booExe.getName().equals("booi")){
            this.startBooTaskWithMonoAndLinuxShellScript(booExe,monoExe );
        }
        else{
            this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorBooi"));
            this.guiMan.setBooReady(true);
        }
        
        
        
        
        
        

    }

    private void startBooTaskWithNet() {
        compilerResult = "";
        currentProcess = null;
        processStarted = false;

        path = new File(taskFile.getParent());
        

        File booExe = new File(pathToBoo);
        if (booExe.exists() && booExe.canExecute() && (booExe.getName().equals("booi.exe")||booExe.getName().equals("booi"))) {
            pb = new ProcessBuilder(pathToBoo, taskFile.getName());
            pb.redirectErrorStream(true);
            pb.directory(path);

            try {
                currentProcess = pb.start();
                processStarted = true;
            } catch (IOException ex) {
                this.guiMan.setOutputInFrame(this.compilerResult);
                this.guiMan.setBooReady(true);
                this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorInterpreting"));
                Logger.getLogger(BooInterpreter.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Error, startBooTask failed (Interpreter): " + ex.toString());
            }

        } else {
            this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorBooi"));
            this.guiMan.setBooReady(true);
        }

    }
}
